package com.my.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;

public class HdfsUpDownload {

    private static final Configuration conf;

    static {
        conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop102:8020");
        System.out.println("conf初始化完成");

        //KERBEROS验证
        /*
        conf.set("hadoop.security.authentication","KERBEROS");
        conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
        conf.set("fs.hdfs.impl.disable.cache","true");
        conf.set("dfs.client.use.datanode.hostname","true");
        conf.addResource(new Path("hdfsconfig/core-site.xml"));
        conf.addResource(new Path("hdfsconfig/hdfs-site.xml"));
        System.setProperty("java.security.krb5.conf","hdfsconfig/krb5.conf");
        UserGroupInformation.setConfiguration(conf);
        try {
            UserGroupInformation.loginUserFromKeytab("用户名","hdfsconfig/xxxx.keytab");
        } catch (IOException e) {
            System.out.println("登录失败");
        }
         */
    }

    public static void main(String[] args) {
        upload("src/main/resources/file/123.pdf","/file/book/");
        download("/file/linux命令.txt");
    }

    public static void upload(String filePath, String remotePath) {
        FileSystem fileSystem = null;
        try {
            fileSystem = FileSystem.get(new URI("hdfs://hadoop102:8020"), conf, "root");
            fileSystem.copyFromLocalFile(false,true,new Path(filePath),new Path(remotePath));

            System.out.println("HDFS:上传完成");
        } catch (Exception e) {
            System.out.println("文件上传出现异常：" + e.getMessage());
        } finally {
            try {
                if (fileSystem != null) fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void download(String filePath) {
        FileSystem fileSystem = null;
        FSDataInputStream in = null;
        FileOutputStream out = null;
        try {
            fileSystem = FileSystem.get(new URI("hdfs://hadoop102:8020"), conf, "root");
            Path path = new Path(filePath);
            if (fileSystem.exists(path)) {
                System.out.println("HDFS:文件存在");
            }
            in = fileSystem.open(path);
            File file = new File("D:\\cache" + File.separator + filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            out = new FileOutputStream(file);

            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            System.out.println("HDFS:下载完成");
        } catch (Exception e) {
            System.out.println("文件下载出现异常：" + e.getMessage());
        } finally {
            try {
                if (out != null) out.close();
                if (in != null) in.close();
                if (fileSystem != null) fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
